package android.TaskApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity 
{
	EditText password;
	EditText firstname;
	EditText username;
	EditText lastname;
	Button submit;
	Toast t_pass;
	Toast t_fail;
	UserResponse x;
	String email;
	String upass;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.signupform);
				
		username= (EditText) findViewById(R.id.useremail);
		password= (EditText) findViewById(R.id.userpassword);
		firstname= (EditText) findViewById(R.id.firstname);
		lastname= (EditText) findViewById(R.id.lastname);
		submit= (Button) findViewById(R.id.signup_submit);
		String t_pass_message="Sign up Success";
		String t_fail_message="Sorry, Sign up failed";
	   t_pass = Toast.makeText(this,t_pass_message, Toast.LENGTH_LONG);
	   t_fail = Toast.makeText(this,t_fail_message, Toast.LENGTH_LONG);
	   
		submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				String last_name=lastname.getText().toString();
				String email= username.getText().toString();
				String first_name=firstname.getText().toString();
				String upass=password.getText().toString();
				x = WebServiceHelper.userSignUp(email, upass, first_name, last_name);
				if(x.getStatusCode()>0)
				{ 
				 t_pass.show();
				    UserResponse respo = WebServiceHelper.UserLogin(email,upass); 
			        UserVO[] user= respo.getUsers();
					int id=user[0].getUserId();
					
					UserGroupResponse ug_home    = WebServiceHelper.createGroup(id,"Home");
					UserGroupResponse ug_office  = WebServiceHelper.createGroup(id,"Office");
					UserGroupResponse ug_general = WebServiceHelper.createGroup(id,"General");
					
					if(ug_home.getStatusCode() <0) Toast.makeText(SignUpActivity.this, "Failed to creat Home group", Toast.LENGTH_LONG).show();
					if(ug_office.getStatusCode() <0) Toast.makeText(SignUpActivity.this, "Failed to creat Office group", Toast.LENGTH_LONG).show();
					if(ug_general.getStatusCode() <0) Toast.makeText(SignUpActivity.this, "Failed to creat General group", Toast.LENGTH_LONG).show();

					finish();					
				}
				else 
				{
				 t_fail.show();
				 finish();
				}
			}
			
		});

		
		}

		
	
	
}
